package com.hoshiicloud.market.controller.activities;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.market.entity.activities.Activities;
import com.hoshiicloud.market.entity.activities.ActivitiesGoods;
import com.hoshiicloud.market.param.activities.ActivitiesGoodsAddParam;
import com.hoshiicloud.market.param.activities.ActivitiesGoodsQueryParam;
import com.hoshiicloud.market.param.activities.ActivitiesGoodsUpdateParam;
import com.hoshiicloud.market.service.activities.ActivitiesGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author campchen
 * @date in 下午 05:07 2019/8/26
 * @packagename com.hoshiicloud.market.controller.activities
 * @description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/activities-goods")
@Api(value = "预售促销", tags = {"预售促销"})
public class ActivitiesGoodsController {

    private final ActivitiesGoodsService activitiesGoodsService;

    public ActivitiesGoodsController(ActivitiesGoodsService activitiesGoodsService) {
        this.activitiesGoodsService = activitiesGoodsService;
    }

    /**
     * 分页查询预售促销
     *
     * @param param param
     * @return com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "分页查询预售促销")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ActivitiesGoodsQueryParam param) {

        try {
            QueryWrapper<ActivitiesGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getActivityId() != null && param.getActivityId() != 0), ActivitiesGoods::getActivityId,
                            param.getActivityId())
                    .eq((param.getSpuId() != null && param.getSpuId() != 0), ActivitiesGoods::getSpuId, param.getSpuId())
                    .eq((param.getSkuId() != null && param.getSkuId() != 0), ActivitiesGoods::getSkuId, param.getSkuId())
                    .eq((param.getUnitprice() != null && param.getUnitprice() != 0), ActivitiesGoods::getUnitprice, param.getUnitprice())
                    .eq((param.getActPersonal() != null), ActivitiesGoods::getActPersonal, param.getActPersonal());
            IPage<ActivitiesGoods> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(activitiesGoodsService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询预售促销异常", e);
            return Response.failed(e.getMessage());
        }

    }

    /**
     * 根据id查询预售促销
     *
     * @param id id
     * @return com.hoshiicloud.common.rpc.Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询预售促销")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(activitiesGoodsService.getById(id));
    }


    /**
     * 新增
     *
     * @param param param
     * @return com.hoshiicloud.common.rpc.Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增预售促销商品")
    public Response save(@RequestBody @Valid ActivitiesGoodsAddParam param) {
        // 将字符串转化为对象：Strategies strategies = JSONObject.parseObject(param.getStrategies().toString(), Strategies.class);
        ActivitiesGoods data;
        try {
            data = BeanUtils.covertType(param, ActivitiesGoods.class);
        } catch (Exception e) {
            log.error("新增预售促销Bean转换异常 {0}", e);
            return Response.failed("新增预售促销Bean转换异常");
        }
        return Response.data(activitiesGoodsService.save(data));

    }

    /**
     * 修改预售促销
     *
     * @param param param
     * @return com.hoshiicloud.common.rpc.Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改预售促销")
    public Response update(@RequestBody @Valid ActivitiesGoodsUpdateParam param) {
        ActivitiesGoods data;
        try {
            data = BeanUtils.covertType(param, ActivitiesGoods.class);
        } catch (Exception e) {
            log.error("修改预售促销Bean转换异常 {0}", e);
            return Response.failed("新增预售促销Bean转换异常");
        }
        return Response.data(activitiesGoodsService.updateById(data));
    }
}
